<?php

namespace app\store\controller;

use library\Controller;
use app\store\service\GoodsService;
use app\store\service\RedGoodsService;
use think\Db;
use think\exception\HttpResponseException;

/**
 * 预警商品
 * Class RedGoods
 * @package app\store\controller
 * @date 2019/03/27 14:43
 */
class RedGoods extends Controller
{


    /**
     * 定义当前操作表名
     * @var string
     */
    public $table = 'StoreRedGoods';


    /**
     * 普通商品
     * @return array|string
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function index()
    {
        $this->title = '预警商品';
        $this->_query($this->table)->like('title')->equal('mid')->order('red desc,total')->page();
    }

    public function referedata(RedGoodsService $goodsService)
    {
        $list = $goodsService->GetList();
        Db::name($this->table)->delete(true);
        Db::name($this->table)->insertAll($list);
        return $this->success('更新成功');
    }

    public function add_order()
    {
        $sns = $this->request->post('sns');
        if (empty($sns)) {
            $this->error('数据异常');
        }
        $sns_array = explode(',', $sns);
        return $this->success('获取成功,生成逻辑待完成');
    }

    /**
     * 数据列表处理
     * @param array $data
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    protected function _index_page_filter(&$data)
    {
       $this->mlist = $this->clist = Db::name($this->table)->group('merchname')->field('mid,merchname')
            ->select();
    }
}
